load("@aspect_rules_esbuild//esbuild:defs.bzl", "esbuild")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("//tools:defaults.bzl", "http_server", "ng_project", "sass_binary")

package(default_visibility = ["//visibility:public"])

npm_link_all_packages()

# List of dependencies that are referenced in the `index.html` file.
devserverIndexHtmlDependencies = [
    "//:node_modules/zone.js",
    "//:node_modules/kagekiri",
    "//src/material/prebuilt-themes:azure-blue",
    ":index.html",
    ":theme",
]

exports_files([
    "protractor.conf.js",
    "devserver-configure.js",
])

ng_project(
    name = "e2e-app",
    testonly = True,
    srcs = glob(
        ["**/*.ts"],
    ),
    assets = glob(
        [
            "**/*.html",
            "**/*.css",
        ],
        exclude = ["index.html"],
    ),
    deps = [
        "//:node_modules/@angular/animations",
        "//:node_modules/@angular/core",
        "//:node_modules/@angular/forms",
        "//:node_modules/@angular/platform-browser",
        "//:node_modules/@angular/router",
        "//src/cdk-experimental/scrolling",
        "//src/cdk/overlay",
        "//src/cdk/scrolling",
        "//src/cdk/testing/tests:test_components",
        "//src/components-examples/private",
        "//src/material/core",
        "//src/material/slider",
    ],
)

sass_binary(
    name = "theme",
    src = "theme.scss",
    deps = [
        "//src/material:sass_lib",
        "//src/material-experimental:sass_lib",
    ],
)

esbuild(
    name = "bundles",
    testonly = True,
    srcs = [
        ":e2e-app",
        "//:node_modules/tslib",
        "//src:build-tsconfig",
    ],
    bundle = True,
    config = "//src:esbuild-linked-config",
    entry_point = ":main.js",
    format = "esm",
    metafile = True,
    output_dir = True,
    platform = "browser",
    sourcemap = "external",
    splitting = True,
    # We cannot use `ES2017` or high as that would result in `async/await` not being downleveled.
    # ZoneJS needs to be able to intercept these as otherwise change detection would not work properly.
    target = "es2016",
)

http_server(
    name = "server",
    testonly = True,
    srcs = devserverIndexHtmlDependencies,
    additional_root_paths = [
        "angular_material/",
    ],
    tags = ["manual"],
    deps = [
        ":bundles",
    ],
)
